package com.ldc.api.config;

import com.ldc.model.Config;
import com.ldc.model.base.ApiBean;
import com.ldc.service.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 功能描述
 *
 * @author liudi
 * @since 2023/7/18
 */
@RestController
@RequestMapping("/config")
public class ConfigController {
    @Autowired
    ConfigService configService;

    /**
     * 查询配置
     *
     * @param params
     * @return
     */
    @PostMapping("/query")
    public ApiBean<?> queryConfig(@RequestBody Config  params) {
        List<Config> configList = configService.findListByType(params.getType());
        return ApiBean.success(configList);
    }

    /**
     * 删除配置
     *
     * @param params
     * @return
     */
    @PostMapping("/delete")
    public ApiBean<?> deleteConfig(@RequestBody Config  params) {
        int code = configService.batchDeleteByTypeAndKeyCode(params.getType(), params.getKeyName());
        return ApiBean.success(code > 0);
    }

    /**
     * 新添加配置
     *
     * @param entity
     * @return
     */
    @PostMapping("/add")
    public ApiBean<?> addConfig(@RequestBody Config entity) {
        boolean success = configService.save(entity);
        return ApiBean.success(success);
    }
}
